package com.chu.service.impl;

import com.chu.entity.Role;
import com.chu.mapper.MenuMapper;
import com.chu.mapper.RoleMapper;
import com.chu.service.RoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {


    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Role> selectAllRole() {
        return roleMapper.selectAllRole();
    }

    @Override
    public PageInfo<Role> findAllOrgRole(int page, int size, String name, String deptname, String state) {
        PageHelper.startPage(page,size);
        Map map = new HashMap();
        map.put("name",name);
        map.put("deptname",deptname);
        map.put("state", state);
        List<Role> list = roleMapper.findAllOrgRole(map);
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    //根据部门id查询职位信息
    @Override
    public List<Role> findAllRole(int id) {
        return roleMapper.findAllRole(id);
    }

    @Override
    public int deleteByPrimaryKey(Integer roleid) {
        return 0;
    }

    @Override
    @Transactional
    public int insert(Role record,int[] mid) {
        //先新增角色信息
        int insert = roleMapper.insert(record);
        //新增中间表,需要角色id和菜单id
        Integer roleid = record.getRoleid();
        Map map = new HashMap();
        map.put("rid",roleid);
        map.put("menuids",mid);
        int result = roleMapper.insertMiddle(map);
        return result;
    }

    @Override
    public int insertSelective(Role record) {
        return 0;
    }

    @Override
    public Role selectByPrimaryKey(Integer roleid) {
        //查询角色信息
        Role role = roleMapper.selectByPrimaryKey(roleid);
        //根据角色信息中的角色id查询权限id
        List<Integer> menuId = menuMapper.findMenuId(roleid);
        role.setMenuIdList(menuId);
        return role;
    }

    @Override
    public int updateByPrimaryKeySelective(Role record) {
        return 0;
    }

    @Override
    public int updateByPrimaryKey(Role record) {
        return 0;
    }
}
